/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.flickr.api.response;

import com.ddw.app.bflickr.flickr.api.FlickrPhoto;
import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;

/**
 * Class for a flickr response photos details that is returned from a 
 * flickr photos request. The object contains details to build the 
 * absolute url such as server, farm and unique id etc.
 * @author craig
 *
 */
public class FlickrResponsePhoto implements FlickrPhoto {

	private Log log = LogFactory.createInstance(this.getClass());
	private String farm;
	private String server;
	private String id;
	private String secret;
	
	/**
	 * The url format for a flickr photo
	 */
	private final String URL_FORMAT = "http://farm%s.static.flickr.com/%s/%s_%s.jpg";
	
	/**
	 * Creates a new instance of a response photo including all the photos details.
	 * @param farm the farm that the photo resides on
	 * @param server the server that the photo resides on
	 * @param id the ID number of the photo
	 * @param secret the secret key of the photo
	 */
	public FlickrResponsePhoto(String farm, String server, String id, String secret) {
		setFarm(farm);
		setServer(server);
		setId(id);
		setSecret(secret);
	}
	
	@Override
	public String getUrl() {
		return String.format(URL_FORMAT, this.farm, this.server, this.id, this.secret);
	}
	
	/**
	 * Getter of the server that the photo resides on
	 * @return the server name
	 */
	public String getServer() {
		return this.server;
	}
	
	/**
	 * Setter of the server that the photo resides on
	 * @param server the server name to set
	 */
	public void setServer(String server) {
		if(server == null) {
			log.error("The server address cannot be null");
			throw new IllegalArgumentException("The server address cannot be null");
		}
		if(server.trim().length() == 0) {
			log.error("The server address cannot be empty");
			throw new IllegalArgumentException("The server address cannot be empty");
		}
		this.server = server;
	}

	/**
	 * Getter of the farm that the photo resides on
	 * @return the farm name
	 */
	public String getFarm() {
		return this.farm;
	}
	
	/**
	 * Setter of the farm that the photo resides on
	 * @param farm the farm name to set
	 */
	public void setFarm(String farm) {
		if(farm == null) {
			log.error("The farm cannot be null");
			throw new IllegalArgumentException("The farm cannot be null");
		}
		if(farm.trim().length() == 0) {
			log.error("The farm cannot be empty");
			throw new IllegalArgumentException("The farm cannot be empty");
		}
		this.farm = farm;
	}
	
	/**
	 * Getter of the photos ID number
	 * @return the photos number
	 */
	public String getId() {
		return this.id;
	}
	
	/**
	 * Setter of the photos ID number
	 * @param id the ID number to set
	 */
	public void setId(String id) {
		if(id == null) {
			log.error("The id cannot be null");
			throw new IllegalArgumentException("The id cannot be null");
		}
		if(id.trim().length() == 0) {
			log.error("The id cannot be empty");
			throw new IllegalArgumentException("The id cannot be empty");
		}
		this.id = id;
	}
	
	/**
	 * Getter of the secret key for the photo
	 * @return the secret key for the photo
	 */
	public String getSecret() {
		return this.secret;
	}
	
	/**
	 * Setter for the secret key for the photo
	 * @param secret the secret key to set
	 */
	public void setSecret(String secret) {
		if(secret == null) {
			log.error("The secret cannot be null");
			throw new IllegalArgumentException("The secret cannot be null");
		}
		if(secret.trim().length() == 0) {
			log.error("The secret cannot be empty");
			throw new IllegalArgumentException("The secret cannot be empty");
		}
		this.secret = secret;
	}
	
}
